package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.entity.Word;
import com.example.springboot.mapper.WordMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/word")
public class WordController {
    @Resource
    WordMapper wordMapper;

    @GetMapping("/search")
    public Result<?> searchEnglish(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "英文") String mode,
                                   @RequestParam(defaultValue = "error") String search) {
        Page<Word> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Word> wrapper = Wrappers.lambdaQuery();
        if ("英文".equals(mode)) {
            wrapper.like(Word::getWordstr,search);
        } else {
            wrapper.like(Word::getParaphrase,search);
        }
        Page<Word> wordPage = wordMapper.selectPage(page, wrapper);
        if (null == wordPage) {
            return Result.error("-1","未查询到数据！");
        }
        return Result.success(wordPage);
    }

    @PostMapping("/insert")
    public Result<?> insertWord(@RequestBody Word word) {
        wordMapper.insert(word);
        return Result.success();
    }


}
